<!DOCTYPE html>
<html lang='zh-CN'>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>优惠券规则</title>
    <link rel="icon" href="favicon.ico" type="image/ico">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/materialdesignicons.min.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.min.css" rel="stylesheet">
    <link href="js/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://at.alicdn.com/t/font_1805932_ysrcp4y0uy9.css">
    <link rel="stylesheet" href="css/uploadImg.css">
    <!--日期选择插件-->
    <link rel="stylesheet" href="js/bootstrap-datepicker/bootstrap-datepicker3.min.css">
    <!--时间选择插件-->
    <link rel="stylesheet" href="js/bootstrap-datetimepicker/bootstrap-datetimepicker.min.css">
</head>
<body>
<div class="container-fluid p-t-15">

    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">

                    <div id="toolbar" class="toolbar-btn-action">
                        <form class="form-inline" method="post" id="searchForm">
                            <div class="form-group">
                                <label>优惠券名称:</label>
                                <input type="text" class="form-control" name="couponName">
                            </div>
                            <div class="form-group">
                                <button class="btn btn-info btn_query btn-round" type="button" id="searchFormBtn">查询</button>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-success btn_query btn-round" type="button" id="addBtn">添加优惠券规则</button>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-warning btn_query btn-round" type="button" id="editBtn">编辑优惠券规则</button>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-danger btn_query btn-round" type="button" id="deleteBtn">删除优惠券规则</button>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-primary btn_query btn-round" type="button" id="addProductBtn">优惠券商品管理</button>
                            </div>
                        </form>
                    </div>

                </div>
                <div class="card-body">
                    <table id="grid_data" class="table text-nowrap"></table>
                </div>
            </div>
        </div>
    </div>

    <!--  添加与编辑模态框 -->
    <div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <span>添加/编辑</span>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <div id="error_tips" style="color: #c9302c"></div>
                </div>
                <div class="modal-body">
                    <form id="addOrEditForm" method="post" enctype="multipart/form-data">
                        <div class="row">

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon" id="basic-account">规则名称</span>
                                    <input type="hidden" name="addOrEditStatus" VALUE="0" id="addOrEditStatus" placeholder="添加或编辑图片状态控制">
                                    <input type="hidden" name="id">
                                    <input type="hidden" name="picUrl">
                                    <input type="hidden" name="deleteUrl">
                                    <input type="text" name="couponName" class="form-control">
                                    <span class="mdi mdi-account form-control-feedback" aria-hidden="true"></span>
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon">优惠劵类型</span>
                                    <label class="form-control">
                                        <input type="radio" name="couponType" checked="checked" value="1"/> 满减券
                                        <input type="radio" name="couponType" value="2"/> 商品卷
                                        <input type="radio" name="couponType" value="3"/> 赠送卷
                                        <input type="radio" name="couponType" value="4"/> 新人卷
                                        <input type="radio" name="couponType" value="5"/> 通用券
                                    </label>
                                </div>
                            </div>

                            <div class="col-xs-12">
                            <div class="input-group m-b-10">
                                <span class="input-group-addon">优惠劵面值</span>
                                <input type="text" name="faceValue" class="form-control control" onkeyup="value=value.replace(/[^\d]/g,'').replace(/^[0]+[0-9]*$/gi,'')">
                            </div>
                        </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon">面值条件</span>
                                    <input type="text" name="faceValueDemand" class="form-control" onkeyup="value=value.replace(/[^\d]/g,'')">
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon" id="start-time">开始时间</span>
                                    <input type="text" class="form-control js-datetimepicker" name="startTime" placeholder="请选择开始时间" data-side-by-side="true" data-locale="zh-cn" data-format="YYYY-MM-DD HH:mm:ss" />
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon" id="end-time">结束时间</span>
                                    <input type="text" class="form-control js-datetimepicker" name="endTime" placeholder="请选择结束时间" data-side-by-side="true" data-locale="zh-cn" data-format="YYYY-MM-DD HH:mm:ss" />
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon">领取类型</span>
                                    <label class="form-control">
                                        <input type="radio" name="receiveType" checked="checked" value="1"/> 手动领取
                                        <input type="radio" name="receiveType" value="2"/> 自动领取
                                    </label>
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon">类型</span>
                                    <label class="form-control">
                                        <input type="radio" name="typeId" checked="checked" value="1"/> 普通
                                        <input type="radio" name="typeId" value="2"/> 抢购
                                    </label>
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon">规则排序</span>
                                    <input type="text" name="ruleOrder" class="form-control" onkeyup="this.value=this.value.replace(/[^bai0-9-]+/,'');">
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon">派发优惠券数量</span>
                                    <input type="text" name="couponCount" class="form-control" onkeyup="this.value=this.value.replace(/[^bai0-9-]+/,'');">
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon">优惠券总数</span>
                                    <input type="text" name="couponAmount" class="form-control" onkeyup="this.value=this.value.replace(/[^bai0-9-]+/,'');">
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="main u-main">
                                    <span class="label-left">上传图片</span>
                                    <div class="upload-content">
                                        <div class="content-img">
                                            <ul class="content-img-list"></ul>
                                            <div class="file">
                                                <i class="gcl gcladd"></i>
                                                <input type="file" name="file" accept="image/*" id="upload">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-primary addSaveBtn" id="saveBtn">保存</button>
                        </div>
                    </form>
                </div>

            </div>
        </div>
    </div>

    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/bootstrap-table/bootstrap-table.js"></script>
    <script type="text/javascript" src="js/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
    <script type="text/javascript" src="js/zero2one/zero2one.version.js"></script>
    <script type="text/javascript" src="js/zero2one/zero2one.core.js"></script>
    <!-- bootstrap表单验证插件 -->
    <script type="text/javascript" src="js/bootstrapvalidator/dist/js/bootstrapValidator.js"></script>
    <!-- bootstrap弹出框插件 -->
    <script type="text/javascript" src="js/jquery.bootstrap.min.js"></script>
    <!-- 表单填充插件 -->
    <script type="text/javascript" src="js/jquery-form/jquery.formloader.js"></script>
    <script src="js/uploadImg.js"></script>
    <!--时间选择插件-->
    <script src="js/bootstrap-datetimepicker/moment.min.js"></script>
    <script src="js/bootstrap-datetimepicker/bootstrap-datetimepicker.min.js"></script>
    <script src="js/bootstrap-datetimepicker/locale/zh-cn.js"></script>
    <!--日期选择插件-->
    <script src="js/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
    <script src="js/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script>
    <script type="text/javascript" src="js/main.min.js"></script>
    <script type="text/javascript" src="js/zero2one/zero2one.curd.js"></script>
    <script type="text/javascript">

        //设置表头
        var columns = [
            {checkbox: true},
            {
                field: 'operate',
                title: '操作',
                formatter: function (event, value, row, index) {
                    return  '<a href="#!" class="btn btn-xs btn-purple m-r-5 copy-btn" title="复制规则" data-toggle="tooltip"><i class="mdi mdi-copyright"></i></a>';
                },  // 自定义方法
                events: {
                    'click .copy-btn': function (event, value, row, index) {
                        copyRule(row);
                    }
                }
            },
            {field: 'id',title: '编号'},
            {field: 'couponName',title: '优惠劵名称'},
            {field: 'picUrl',title: '优惠劵图片',formatter: function (value, row, index) {
                    return value != null ? "<img style='width: 150px;height: 50px;' src='"+value+"'/>" : "";
                }},
            {field: 'couponType', title: '优惠劵类型',formatter: function (value, row){
                if (value==1){ return "满减券"
                }else if (value==2){ return "商品卷"
                }else if (value==3){ return "赠送卷"
                }else if (value==4){ return "新人劵"
                }else if (value==5){ return "通用券" }}},
            {field: 'faceValue',title: '优惠劵面值'},
            {field: 'faceValueDemand',title: '面值条件'},
            {field: 'startTime',title: '开始时间',formatter: function (value) { return zero2one.dateFormat(value, 'Y-m-d H:i:s') }},
            {field: 'endTime',title: '结束时间',formatter: function (value) { return zero2one.dateFormat(value, 'Y-m-d H:i:s') }},
            {field: 'receiveType',title: '领取类型',formatter: function (value, row, index) {return value == 1 ? "手动领取": "自动领取"}},
            {field: 'ruleOrder',title: '规则排序'},
            {field: 'couponCount',title: '派发优惠券数量'},
            {field: 'couponAmount',title: '优惠券总数'},
            {field: 'typeId',title: '类型',formatter: function (value, row, index) {return value == 1 ? "普通": "抢购"}},
            {field: 'ruleStatus',title: '规则状态',formatter: function (value, row, index) {
                var checked = value == 1 ? "checked" : "";
                return '<label class="lyear-switch switch-solid switch-success"><input type="checkbox" '+checked+' onclick="status('+value+','+row.id+')"><span></span> '+(value== 1 ? "有效" : "已过期")+' </label>'; }}
        ];

        //复制
        function copyRule(row) {
            var datas = $("#grid_data").bootstrapTable('getSelections');
            if(datas.length == 1){
                zero2one.request("/remote/coupon/copy",{id : row.id},function (data) {
                    window.location.reload();
                });
            }else {
                $.messager.alert("温馨提示", "请选择一条规则进行复制");
            }
        }

        //加载列表信息
        zero2one.table("/remote/coupon/list", columns);

        //根据条件查询列表信息
        zero2one.query();

        //点击编辑按钮弹出模态框
        $("#editBtn").click(function () {
            var datas = $('#grid_data').bootstrapTable('getSelections');
            if(datas.length == 1){
                datas[0].startTime=zero2one.dateFormat(datas[0].startTime, 'Y-m-d H:i:s');
                datas[0].endTime=zero2one.dateFormat(datas[0].endTime, 'Y-m-d H:i:s');
                $("#addOrEditForm").formloader({data: datas[0]});
                $("#addOrEditStatus").val(1);
                if(datas[0].picUrl.length > 0){ addNewContent(".content-img-list", datas[0].picUrl.split(",")); }
                $("#addOrEditModal").modal("show");
            }else{
                $.messager.alert("温馨提示", "请选择一条数据操作");
            }
        });

        //点击添加按钮弹出模态框
        $("#addBtn").click(function () {
            $("#addOrEditForm")[0].reset();
            $("input[type='hidden']").val("");//清空hidden值
            $(".content-img-list").html("");//清空图片回显信息
            $("#addOrEditModal").modal("show");
        });

        //选择图片还没有上传
        var imgFile = uploadImg(".content-img-list", "upload", 1024 * 1024 * 4, 0);

        //点击保存按钮提交请求且提交前先执行表单验证
        $('#addOrEditForm').bootstrapValidator({
            fields: {
                couponName: {validators: {notEmpty: {message: '规则名称不能为空'}}},
                faceValue: {validators: {notEmpty: {message: '优惠劵面值不能为空'}}},
                startTime: {validators: {notEmpty: {message: '开始时间不能为空'}}},
                endTime: {validators: {notEmpty: {message: '结束时间不能为空'}}},
                ruleOrder: {validators: {notEmpty: {message: '规则排序不能为空'}}}
            }
        });

        //添加编辑操作
        $("#saveBtn").click(function () {
            var bv = $('#addOrEditForm').data('bootstrapValidator');
            bv.validate();
            if (bv.isValid()) {
                var formFile = new FormData();
                // 遍历图片imgFile添加到formFile里面
                $.each(imgFile,function (i, v) {
                    formFile.append("file", v)
                });
                handleImage('picUrl', 'deleteUrl');//编辑处理删除的图片
                zero2one.requestU("addOrEditForm","/remote/coupon/addOrEdit",formFile,function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功");
                        window.location.reload();
                    } else { $.messager.alert("温馨提示", data.msg) }
                })
            }
        });

        //点击删除按钮弹出模态框
        $("#deleteBtn").click(function () {
            var datas = $('#grid_data').bootstrapTable('getSelections');
            if(datas.length == 1){
                if (datas[0].ruleStatus === 1) { return $.messager.alert("温馨提示", "状态:开启的数据不能操作");
                } else {
                    $.messager.confirm("温馨提示", "您确定要删除选中的数据吗?", function () {
                        zero2one.request("/remote/coupon/delByIds",{"ids": zero2one.dynamicArray2str(datas,"id"),
                            "urls": zero2one.dynamicArray2str(datas,"picUrl")},function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", "操作成功");
                                $('#grid_data').bootstrapTable('refresh');
                            } else { $.messager.alert("温馨提示", data.msg) }
                        });
                    })
                }
            }else{
                $.messager.alert("温馨提示", "请选择一条数据操作");
            }
        });

        //修改优惠劵规则状态
        function status(value,id){
            zero2one. request("/remote/coupon/status", {"status": value == 1 ? 2 : 1,"id": id},function (data){
                window.location.reload();
            })
        }

    </script>
</div>
</body>
</html>
